/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/6/12
 * Time: 14:27
 */

#include "GameUi.h"

#include "CardUi.h"
#include "HandCardArea.h"
#include "OverlapLayout.h"
#include "Player.h"
#include "TrumpSelect.h"
namespace Upgrade {
    QList<CardSuit> GameUi::suit_order_ = {CardSuit::HeartsSuit,CardSuit::SpadesSuit,CardSuit::DiamondsSuit,CardSuit::ClubsSuit,CardSuit::JokerSuit};

    GameUi::GameUi(const GameSPtr &_game, const PlayerSPtr &_player)
        : game_(_game)
        , player_(_player)
        , trump_select_(new TrumpSelect(this))
        , hand_card_area_(new HandCardArea(this))
    {
        game_->AddPlayer(player_);

        resize(1840, 1080);
        InitWidget();
        InitUi();
        InitEvent();
        UpdateOrderFunction(CardSuit::Unkonw);
    }

    void GameUi::InitUi()
    {
        auto vbox = new QVBoxLayout(this);
        vbox->setContentsMargins(0, 0, 0, 0);

        auto trump_layout = new QHBoxLayout();
        trump_layout->addWidget(trump_select_,0,Qt::AlignHCenter);

        vbox->addStretch(3);
        vbox->addLayout(trump_layout);
        vbox->addWidget(hand_card_area_);
    }

    void GameUi::InitEvent()
    {
        connect(game_.get(), &Game::SendCard, this, &GameUi::Update);

        // 1.初始时所有玩家的trump_select_都可见，各玩家竞争定主
        connect(trump_select_,&TrumpSelect::Select,this,[=](CardSuit _suit) {
            game_->SetMainSuit(player_,_suit);
        });

        // 抢主后的通知
        connect(game_.get(),&Game::IsAlready,this,[=](const PlayerSPtr & _player, CardSuit _suit) {
            // game已经确定主家和花色后 其余玩家trump_select_不可见
            trump_select_->setVisible(false);

            UpdateOrderFunction(_suit);
        });

        // 超时
        connect(game_.get(),&Game::TimeOut,this,[=]() {
            // 超时后所有玩家trump_select_不可见
            trump_select_->setVisible(false);
        });

        connect(game_.get(),&Game::DealFinished,this, [=](const PlayerSPtr & _player) {
            int i = 0;
        });
    }

    void GameUi::InitWidget()
    {
    }

    void GameUi::Update(const PlayerSPtr &_player, const CardSPtr &_card)
    {
        //默认界面为用户A1
        if(_player != player_)
            return;

        // 显示trump选择器
        if(!trump_select_->isVisible())
            trump_select_->setVisible(true);

        if(nullptr == game_->GetMainPlayer()) {
            if(_card->rank == game_->GetMainRank()) {
                _card->SetTrump(true);
                trump_select_->AddSuit(_card->suit);
            }

            if(_card->suit == CardSuit::JokerSuit) {
                _card->SetTrump(true);
                trump_select_->AddJoker(_card->rank);
            }
        }

        hand_card_area_->AddWidget(_card);
    }

    void GameUi::UpdateOrderFunction(CardSuit _currentSuit)
    {
        // 不在倒数第二(倒一为Joker，永不变化)的位置上 则需要重新排序
        while(CardSuit::Unkonw != _currentSuit && suit_order_.at(3) != _currentSuit) {
            auto first = suit_order_.front();
            suit_order_.pop_front();

            suit_order_.insert(3,first);
        }

        hand_card_area_->SetReorderSuit(suit_order_,_currentSuit);
    }
}// namespace Upgrade